package com.jt.chapter06.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author jiang tao
 * @date 2022/1/21
 * @description:
 **/
@Data
public class Result<T> implements Serializable {

    private boolean success;
    private String code;
    private String message;
    private T data;

    public Result() {
        this.success = true;
    }

    public Result(String code, String message) {
        this(true, code, message);
    }

    public Result(boolean success, String code, String message) {
        this(success, code, message, null, (Throwable) null);
    }

    public Result(boolean success, String code, String message, T data) {
        this(success, code, message, data, (Throwable) null);
    }

    public Result(boolean success, String code, String message, T data, Throwable cause) {
        this.success = true;
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> success(String code, String message, T data){
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        result.setSuccess(true);
        return result;
    }

    public static <T> Result<T> success(T data){
        Result result = new Result();
        result.setCode("200");
        result.setMessage("");
        result.setData(data);
        result.setSuccess(true);
        return result;
    }

    public static <T> Result<T> fail(String code, String message){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> fail(String message){
        Result<T> result = new Result<>();
        result.setCode("302");
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
}
